package org.clubJava.calc;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import calculadora.Calculadora;

public class HomePage extends WebPage {

	private static final long serialVersionUID = 1L;
	private Calculadora calc = new Calculadora();
	private TextField display = new TextField("display",
			new PropertyModel(calc,"display"));
	public HomePage() {
		final Form form = new Form("form");
		display.setEnabled(false);
		display.setOutputMarkupId(true);
		form.add(display);

		form.add(new AjaxButton("suma") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				if (!calc.getOperationFlag()) {
					calc.setMemoria(calc.suma(calc.getDisplay(), calc
							.getMemoria()));
					calc.setDisplay(calc.getMemoria());
					calc.setOperationFlag(true);
				}
				target.addComponent(display);
			}
		});
		
		form.add(new AjaxButton("masOrMenos") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				calc.setDisplay(calc.getDisplay()*-1);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("C") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				calc.setDisplay(0);
				calc.setMemoria(0);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("1") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				getDigit(1);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("2") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				getDigit(2);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("3") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				getDigit(3);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("4") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				getDigit(4);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("5") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				getDigit(5);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("6") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				getDigit(6);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("7") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target,final Form form) {
				getDigit(7);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("8") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				getDigit(8);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("9") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target, final Form form) {
				getDigit(9);
				target.addComponent(display);
			}
		});
		form.add(new AjaxButton("0") {
			private static final long serialVersionUID = 1L;
			protected void onSubmit(final AjaxRequestTarget target,final Form form) {
				getDigit(0);
				target.addComponent(display);
			}
		});

		add(form);
	}

	private void getDigit(final int num) {
		if (calc.getOperationFlag()) {
			calc.setOperationFlag(false);
			calc.setDisplay(num);
			return;
		}
		if (calc.getDisplay() >= 0) {
			calc.setDisplay((calc.getDisplay() * 10) + num);
		} else {
			calc.setDisplay((calc.getDisplay() * 10) - num);
		}
	}
}
